/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package src.services;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import javax.faces.model.SelectItem;
import src.Menu;



/**
 *
 * @author Yusup Jauhari Shandi <in.visi>
 */
public class MenuModel {
  private CRUEDService service;
    
    public MenuModel() {
        service = new CRUEDService();
    }
    
    public List<Menu> findAll(Map<String, Object> filters) {
        return (List<Menu>) service.findAllSQLAdvance("from Menu as menu ", filters," order by menu.menuId");
    }
    
    public List<Object[]> findSecurity(Map<String, Object> filters) {
        String sql = "from Menu as menu , Ugpolicy as policy ";
        String where = " where menu.menuId = policy.id.menuId ";
        if (filters!=null) {
            for (Iterator<Entry<String, Object>> it = filters.entrySet().iterator(); it.hasNext();) {
                Map.Entry<String, Object> e = it.next();
                where += (where.length()>0 ? " AND " : "") + " " + e.getKey()  + e.getValue() ;
            }
            }
        sql += where;
        //return (List<Menu>) service.findAllSQLAdvance(sql, null," order by menu.menuId");
        return (List<Object[]>) service.findAllSQLAdvance(sql, null," order by menu.menuId");
    }
    
    public List<Menu> findAll2(Map<String, String> filters) {
        return (List<Menu>) service.findAll("Menu", filters);
    }
    
    public List<SelectItem> listAll() {
        return service.listAll("Menu", "MenuId","MenuName");
    }

    public boolean saveData(Menu data) {
        return service.saveData(data);
    }
    
    public boolean deleteData(Menu data) {
        return service.deleteData(data);
    }    
}
